using QFramework;
using UnityEngine;
using System;
using System.Collections.Generic;

namespace HareGame
{
    [MonoSingletonPath("GameEntry/TaskManager")]
    public class TaskManager : MonoBehaviour_Ex, ISingleton
    {
        List<TaskData> _allTaskDatas = new List<TaskData>();
        List<TaskData> _curTaskDatas = new List<TaskData>();
        [SerializeField]
        public int _curStateID;

        public int MaxState;

        /// <summary>
        /// 初始化
        /// </summary>
        public void OnSingletonInit()
        {
            _allTaskDatas = GameEntry.Data.GetAllTaskData();
            _curStateID = GameEntry.GameData.GetInt(UserData.TaskStateID, 1);
            foreach (var v in _allTaskDatas)
            {
                if (v.StateID > MaxState)
                {
                    MaxState = v.StateID;
                }
            }
        }

        /// <summary>
        /// 定义单例
        /// </summary>
        public static TaskManager Instance
        {
            get { return MonoSingletonProperty<TaskManager>.Instance; }
        }

        public TaskData GetFirstTaskData()
        {
            GetStateTaskDatas();
            foreach (var v in _curTaskDatas)
            {
                if (!v.IsCompleted)
                {
                    return v;
                }
            }
            return null;
        }

        /// <summary>
        /// 获得现阶段打开ui以后要显示的任务;
        /// </summary>
        public List<TaskData> GetStateShowTaskDatas()
        {
            _curTaskDatas = GetStateTaskDatas();
            List<TaskData> NowShowTaskData = new List<TaskData>();
            foreach (var item in _curTaskDatas)
            {
                if (item.IsCompleted)
                {
                    continue;
                }
                else
                {
                    if (NowShowTaskData.Count >= 3)
                    {
                        continue;
                    }
                    NowShowTaskData.Add(item);
                }
            }
            return NowShowTaskData;
        }

        /// <summary>
        /// 检测是否存在可完成任务
        /// </summary>
        /// <returns></returns>
        public bool HasReady2CompleteTask()
        {
            foreach (var v in _curTaskDatas)
            {
                if (!v.IsCompleted && v.IsReady2Complete)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 获取阶段任务列表
        /// </summary>
        /// <param name="stateID">阶段ID</param>
        /// <returns></returns>
        public List<TaskData> GetStateTaskDatas()
        {
            _curTaskDatas.Clear();
            foreach (var v in _allTaskDatas)
            {
                if (v.StateID == _curStateID)
                {
                    _curTaskDatas.Add(v);
                }
            }
            return _curTaskDatas;
        }

        /// <summary>
        /// 前往任务目标
        /// </summary>
        /// <param name="data">任务数据</param>
        public void OnTaskGoAction(TaskData data)
        {
            switch (data.Type)
            {
                //购买建筑
                case 1:

                    break;
                //建筑升级
                case 2:

                    break;
                //购买设施
                case 3:

                    break;
            }

        }

        /// <summary>
        /// 是否能完成阶段奖励
        /// </summary>
        /// <returns></returns>
        public bool IsReady2CompleteStateTask()
        {
            foreach (var v in _curTaskDatas)
            {
                if (!v.IsCompleted)
                {
                    return false;
                }
            }
            return true;
        }

        public bool IsCompleteAllTask => _curStateID > MaxState;

        /// <summary>
        /// 完成任务
        /// </summary>
        /// <param name="data"></param>
        public void CompleteTask(TaskData data)
        {
            data.OnTaskComplete();
        }

        /// <summary>
        /// 完成阶段任务
        /// </summary>
        public void CompleteStateTask()
        {
            _curStateID++;
            GameEntry.GameData.SetInt(UserData.TaskStateID, _curStateID);
            GameEntry.Event.Send(EventID.RefreshTaskUI);
        }
    }
}